package com.tongcao.cn.listener.convert;

import com.alibaba.nacos.api.config.convert.NacosConfigConverter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tongcao.cn.listener.nacos.Pojo;

import java.io.IOException;

/**
 * @author:tongcao
 * @since:2019/7/26 15:51
 * Description:
 */
public class PojoNacosConfigConverter implements NacosConfigConverter<Pojo> {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public boolean canConvert(Class<Pojo> targetType) {
        return objectMapper.canSerialize(targetType);
    }

    @Override
    public Pojo convert(String config) {
        try {
            return objectMapper.readValue(config, Pojo.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}